<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="角色Code" width="150">
        <template slot-scope="scope">
          {{ scope.row.code }}
        </template>
      </el-table-column>
      <el-table-column label="角色描述" width="150">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="300">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createdAt }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="更新时间" width="300">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.updatedAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            circle
            @click=" handelDelete(scope.row.code)"
          />
          <el-button size="mini" type="primary" icon="el-icon-edit" circle @click="dialogFormVisible=true; updateRole.code = scope.row.code" />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData"
    />

    <el-dialog title="修改角色" :visible.sync="dialogFormVisible">
      <el-form ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色Code">
          <el-input v-model="updateRole.input.newCode" />
        </el-form-item>
        <el-form-item label="描述信息">
          <el-input v-model="updateRole.input.description" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getManagementClient } from '@/utils/authing-util'

export default {
  data() {
    return {
      dialogFormVisible: false,
      updateRole: {
        code: '',
        input: {
          newCode: '',
          description: ''
        }
      },
      list: null,
      listLoading: true,
      keyword: '',
      page: 1,
      size: 10,
      total: 0
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    handleUpdate() {
      console.log(this.updateRole.code)
      getManagementClient().roles.update(this.updateRole.code,
        { newCode: this.updateRole.input.newCode, description: this.updateRole.input.description }).then(res => {
        this.fetchData(this.page)
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      }).catch((err) => {
        this.$message({
          type: 'error',
          message: `修改失败,错误信息:${err.message}`
        })
      })
    },
    handelDelete(code) {
      this.$confirm('此操作将删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        getManagementClient().roles.delete(code, '626bfa7676640382aee27c74').then(res => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      getManagementClient().roles.list({ page: this.page, limit: this.size, namespace: '626bfa7676640382aee27c74' }).then(res => {
        this.listLoading = false
        this.list = res.list
        this.total = res.totalCount
      })
    }
  }
}
</script>
